package com.example.service;

import com.example.mapper.ClickLikeMapper;
import com.example.mapper.JobSeekerMapper;
import com.example.pojo.dao.MongoIdRepository;
import com.example.pojo.dao.ResponseRepository;
import com.example.pojo.entity.JobSeeker;
import com.example.pojo.entity.MongoId;
import com.example.pojo.entity.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/21
 */
@Service
public class ResponseServiceImpl implements ResponseService{
    @Autowired
    private ResponseRepository responseRepository;
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Autowired
    private MongoIdRepository mongoIdRepository;
    @Autowired
    private ClickLikeMapper clickLikeMapper;
    @Override
    public List<Response> findAllByCommentId(String commentId) {
        List<Response> list = responseRepository.findAllByCommentIdOrderByLikeNum(commentId);

        return list;
    }

    @Override
    public int saveResponse(Integer jsId, String commentId,String responseContent,String optionJsName) {
        JobSeeker jobSeeker = jobSeekerMapper.findJSById(jsId);
        Response response = new Response();
        response.setResponse(responseContent);
        response.setJsId(jsId);
        response.setAvatar(jobSeeker.getAvatar());
        response.setCommentId(commentId);
        response.setLikeNum(0);
        response.setOptionJsName(optionJsName);
        response.setCreateTime(new Date());
        response.setJsName(jobSeeker.getJsName());
        responseRepository.save(response);
        MongoId mongoId = mongoIdRepository.findById("6532b1421144d177747c8610").orElse(null);
        if (mongoId!=null){
            mongoId.setResponseId(mongoId.getResponseId()+1);
            mongoIdRepository.save(mongoId);
        }
        return 1;
    }

    @Override
    public int addResponseLikeNum(Integer jsId, String responseId) {
        Response response = responseRepository.findById(responseId).orElse(null);
        if (response!=null){
            response.setLikeNum(response.getLikeNum()+1);
            responseRepository.save(response);
            clickLikeMapper.save(jsId,responseId,2);
        }
        return 1;
    }

}
